<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/normalize.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/mui.css"/>
	</head>
	<style type="text/css">
		body{
			font-size: 3.2rem;
			line-height: 4rem;
			padding: 10px;
		}
	</style>
	<body>
		<div id="oauth_all"></div>
		<a href="../main/print.html" class="mui-btn">打印页面</a>
	</body>
	<script src="../../js/mui.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../js/signIn/signIn.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
			function plusReady(){
				plus.oauth.getServices(function(services){
					var content=document.getElementById('dcontent');
					var info=document.getElementById("info");
					var txt="登录认证通道信息：";
					for(var i in services){
						var service=services[i];
						console.log(service.id+": "+service.authResult+", "+service.userInfo);
						auths[service.id]=service;
						console.log(JSON.stringify(auths));
						txt += "id:"+service.id+", ";
						txt += "description:"+service.description+", ";
						console.log("txt",txt)
						var de=document.createElement('div');
						de.setAttribute('class','mui-btn');
						de.setAttribute('onclick','login(this.id)');
						de.id=service.id;
						de.innerText=service.description+"登录";
						console.log(service.id)
						oauth_all.appendChild(de);
					}
//					info.innerText=txt;
				},function(e){
					outLine("获取登录认证失败："+e.message);
				});
			}
			var auths = {};
			document.addEventListener('plusready',plusReady,false);
			function login(id){
//				outSet("----- 登录认证 -----");
				var auth=auths[id];
				console.log("auths",JSON.stringify(auths))
				console.log("auth",JSON.stringify(auth))
				if(auth){
					var w=null;
					if(plus.os.name=="Android"){
						w=plus.nativeUI.showWaiting();
					}
					document.addEventListener("pause",function(){
						setTimeout(function(){
							w&&w.close();w=null;
						},2000);
					}, false );
					auth.login(function(){
						w&&w.close();w=null;
						console.log("认证成功!");
						userinfo(auth);
					},function(e){
						w&&w.close();w=null;
						console.log("认证失败!");
						plus.nativeUI.alert("详情错误信息请参考授权登录(OAuth)规范文档：http://www.html5plus.org/#specification#/specification/OAuth.html",null,"登录失败["+e.code+"]："+e.message);
					});
				}else{
					outLine("无效的登录认证通道！");
					plus.nativeUI.alert("无效的登录认证通道！",null,"登录");
				}
			}
			// 获取用户信息
			function userinfo(a){
				a.getUserInfo(function(){
					var nickname=a.userInfo.nickname||a.userInfo.name||a.userInfo.miliaoNick;
					plus.nativeUI.alert("欢迎“"+nickname+"”登录！");
				},function(e){
					plus.nativeUI.alert("获取用户信息失败！",null,"登录");
				});
			}
		</script>
</html>
